<?

	include("inc/config.inc.php");

	if ($_GET[a]=="logout")
	{
		unset($_SESSION[user]);
		header("Location:index.php");
		exit();
	}

	if (isSet($_POST[u]) && isSet($_POST[p]))
	{
		$mailauth = @imap_open($config[mailauth], $_POST[u], $_POST[p]);
		
		if ($mailauth)
		{
			$_SESSION[user] = strtolower(mysqli_real_escape_string($con,$_POST[u]));
			header("Location:main.php");
			exit();
		}
		else
		{
			header("Location:login.php?u=$_POST[u]&s=failed");
			exit();
		}
	}

	else
	{
		include("inc/header.inc.php");

		echo "<h2><span class='textColorChange'>$config[systemtitle]</span></h2><br/>\n\n";

?>
	<form method="POST" action="login.php">
	Username: <input type="text" name="u" value="<?= $_GET[u] ?>"><br/><br/>
	Password: &nbsp;<input type="password" name="p"> <input type="submit" value=" Login ">
	</form>

<?
		if ($_GET[s]=="failed")
			echo "<br/><font color='red'>Login failed. Please Try again</font>";

		include("inc/footer.inc.php");
	}

?>
